package com.contact;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.adpter.PlaceAdapter;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.common.TranHelper;
import com.app.DemoApplication;
import com.common.method.CommonData;
import com.common.method.FromFlag;
import com.matang.R;
import com.modle.PlaceStruct;
import com.util.AppUtil;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:PlaceChooseActivity
 * @Description:TODO选择地点
 * @author zhuw
 * @date 2013-8-12 下午8:47:36
 */
public class BasePlaceChooseActivity extends BaseActivity implements
		OnClickListener {
	ListView listView;
	LinearLayout gpsCon;
	ArrayList<PlaceStruct> placeList;
	PlaceAdapter adapter;
	int fromFlag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_place_choose);

		init();
	}

	void init() {
		mContext = BasePlaceChooseActivity.this;
		findViews();
		initPaneData();

	}

	public void initPaneData() {
		super.initPaneData();

		

		// 判断是否从上一个界面传入的数据
		fromFlag = getIntent().getIntExtra("fromFlag", 0);
		if (fromFlag == FromFlag.PLACE_F_USERSEARCH
				|| fromFlag == FromFlag.PLACE_F_JOBSEARCH) {
			gpsCon.setVisibility(View.VISIBLE);
		} else {
			gpsCon.setVisibility(View.GONE);
			
		}
		gpsCon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					Bundle b=new Bundle();
				if (fromFlag == FromFlag.PLACE_F_USERSEARCH) {
					b.putString("GPS", CommonData.GPS);
					TranHelper.toActClearTop(mContext,
							UserSearchActivity.class, b);

				} else if (fromFlag == FromFlag.PLACE_F_JOBSEARCH) {
					b.putString("GPS", CommonData.GPS);
//					TranHelper.toActClearTop(mContext,
//							JobSearchActivity.class, b);
				} 
				
			}
		});
		Serializable obj = getIntent().getSerializableExtra("placeList");
		if (obj != null) {
			placeList = (ArrayList<PlaceStruct>) getIntent()
					.getSerializableExtra("placeList");
			if (!StringUtil.isListNull(placeList)) {
				fillList();
			} else {
				AppUtil.tsMsg(mContext, "无数据");
				finish();

			}
		} else {
			try {

				if (StringUtil.isListNull(DemoApplication.placeList)) {
					placeList = JsonUtil.readPlaceTree(new JSONArray(mContext
							.getString(R.string.new_place_str)));
				} else {
					placeList = DemoApplication.placeList;
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
			fillList();

		}

	}

	@Override
	protected void findViews() {
		super.findViews();
		listView = (ListView) findViewById(R.id.listview);
		gpsCon = (LinearLayout) findViewById(R.id.main_skill_con);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				PlaceStruct ps = placeList.get(pos);
				if (StringUtil.isListNull(ps.childList)) {
					// 没有子类
					Bundle b = new Bundle();
//					if (fromFlag == FromFlag.PLACE_F_MR_BIRTHPLACE) {
//						b.putSerializable("birthPlaceStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								UserResumeModifyActivity.class, b);
//					} else if (fromFlag == FromFlag.PLACE_F_MR_LIVEPLACE) {
//						b.putSerializable("livePlaceStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								UserResumeModifyActivity.class, b);
//					} else if (fromFlag == FromFlag.PLACE_F_MR_ORG_BREFINFO) {
//						b.putSerializable("locationPlaceStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								OrgBriefInfoModifyActivity.class, b);
//					} else if (fromFlag == FromFlag.PLACE_F_USERSEARCH) {
//						b.putSerializable("placeStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								UserSearchActivity.class, b);
//
//					} else if (fromFlag == FromFlag.PLACE_F_JOBSEARCH) {
//						b.putSerializable("placeStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								JobSearchActivity.class, b);
//					} else if (fromFlag == FromFlag.PLACE_F_RESUME_FILTER) {
//						b.putSerializable("placeStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								ResumeFilterActivity.class, b);
//					} else if (fromFlag == FromFlag.PLACE_F_USER_INFO) {
//						b.putSerializable("placeStr", ps.name);
//						TranHelper.toActClearTop(mContext,
//								UserInfoActivity.class, b);
//					}

				} else {
					Bundle b = new Bundle();
					b.putSerializable("placeList", ps.childList);
					b.putInt("fromFlag", fromFlag);
					TranHelper
							.toAct(mContext, BasePlaceChooseActivity.class, b);

				}

			}
		});

	}

	private void fillList() {
		if (StringUtil.isListNull(placeList)) {

		} else {
			if (adapter == null) {
				adapter = new PlaceAdapter(placeList, mContext);
				listView.setAdapter(adapter);
			}

		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit_btn: {
			// 提交登录
			break;
		}

		}

	}

}
